<?php
/***********************
Henson
2011-6-16
分页类
***********************/
class PageClass {
	var $total = 0;  //总记录数
	var $curPage = 1;  //当前页
	var $showNum = 5;  //显示记录数
	var $totalPage = 0;  //总页数
	var $limitStart = 0;
	var $link = '';  //链接地址
	var $pageList = '';
	var $pageMax = 10;//最多显示页面数

	function PageClass($total,$curPage,$showNum,$link)
	{
		if(is_numeric($total) && is_numeric($curPage) && is_numeric($showNum))
		{
			$this->total = $total;
			$this->curPage = $curPage;
			$this->showNum = $showNum;
			$this->link = $link;
			if($this->total > 0){
				$this->doPage();
			}
		}
	}
	function doPage()
	{
		$this->totalPage = ceil($this->total / $this->showNum);
		if($this->totalPage < 1)$this->totalPage = 1;
		if($this->curPage > $this->totalPage){
			$this->curPage = $this->totalPage;
		}
		$curPg = $this->curPage;
		$this->limitStart = ($curPg - 1) * $this->showNum;
		if($curPg <= 5){$startp = 1;}else{$startp = $curPg -5;}
		if($startp+10 > $this->totalPage){
			$endp = $this->totalPage;
		}else{
			$endp = $startp+10;
		}
		$this->pageList .= '<ul>';
		if($startp > 1)
			$this->pageList .= $this->writeFLPage('first');
		for($i=$startp; $i<=$endp; $i++){
			$this->writePageList($i);
		}
		if($endp < $this->totalPage)
			$this->pageList .= $this->writeFLPage('last');
		$this->pageList .= '</ul>';
	}
	//
	function writePageList($num)
	{
		if($num == $this->curPage){
			$this->pageList .= '<li class="page_sel">'.$num.'</li>';
		}else{
			if(strpos($this->link,'{{p}}')){
				$this->pageList .= '<li class="page_link"><a href="'.str_replace('{{p}}', $num, $this->link).'">'.$num.'</a></li>';
			}else{
				if(strpos($this->link,'.php?')){
					$link = $this->link.'&p='.$num;
				}else{
					$link = $this->link.'?p='.$num;
				}
				$this->pageList .= '<li class="page_link"><a href="'.$link.'">'.$num.'</a></li>';
			}
		}
	}

	function writeFLPage($type)
	{
		if($type == 'first'){
			$showPage = '1';
			$showStr = '首页';
		}else{
			$showPage = intval($this->totalPage);
			$showStr = '尾页';
		}

		$str = '';
		if(strpos($this->link,'{{p}}')){
			$str .= '<li class="page_link"><a href="'.str_replace('{{p}}', $showPage, $this->link).'">'.$showStr.'</a></li>';
		}else{
			if(strpos($this->link,'.php?')){
				$link = $this->link.'&p='.$showPage;
			}else{
				$link = $this->link.'?p='.$showPage;
			}
			$str .= '<li class="page_link"><a href="'.$link.'">'.$showStr.'</a></li>';
		}
		return $str;
	}
}
?>